package com.chuntiao.hgc;



import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author chuntiao-WZY菜狗
 * 
 */
public class MyHGCListener {
	static OkHttpClient client = new OkHttpClient();

	/**
	 * 
	 * 自动监控 哈工程 http://yzb.hrbeu.edu.cn/2019/0320/c3262a219914/page.htm 计算机科学与技术学院的调剂信息是否开通
	 * 我可不敢保证它完全准确（比如网页地址可能会变），所以此程序仅仅当个玩具玩一下，如果你不相信它，请自己去刷新网页吧。
	 * 如果你觉得这个玩具还有趣，请点击右上角 Start
	 */
	public static void main(String[]args) {
		
		String url = "http://yzb.hrbeu.edu.cn/2019/0320/c3262a219914/page.htm";
		Request request=new Request.Builder()
				.url(url)
				.build();
		
		
		while (true) {
			try {
				Call call = client.newCall(request);
				Response response = call.execute();
				Document document = Jsoup.parse(response.body().string());
				
				Element school = document.select("#container > div > div > div.entry > div > div > div > table > tbody > tr:nth-child(7) > td:nth-child(2) > p > span").first();
				Element status = document.select("#container > div > div > div.entry > div > div > div > table > tbody > tr:nth-child(7) > td:nth-child(3) > p > span").first();
				System.out.println(school.html()+" -> "+status.html());
				
				if(!status.html().equals("暂未开通"))
					break;
				
				Thread.sleep(1000 *60*5); //刷新频率现在为5分钟，请合理使用（以防被当成DDoS）
			} catch (IOException | InterruptedException e) {
				e.printStackTrace();
			}
			
	}	
		
		File file=new File("C:\\Users\\chuntiao\\Music\\才旦卓玛 - 天路.mp3");//这里换成你喜欢的歌曲，当网页发生变化时，会播放
		
		try {
			FileInputStream fis=new FileInputStream(file);
			BufferedInputStream stream=new BufferedInputStream(fis);
			Player player=new Player(stream);
			player.play();
			
		} catch (JavaLayerException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		
		
	}
}
